package com.whcoding.multithreading.thred;

import org.junit.Test;

/**
 * @program: spring-boot-learning
 * @description:Runnable Runnable创建多线程
 * @author: whcoding
 * @create: 2022-06-09 14:35
 **/
public class MyThreadCreateRunnable {

	/**
	 * @param
	 */
	@Test
	public void MyThreadCreateRunnableTest() {

		class MyRunnable implements Runnable {
			private int ticketNumber = 50;

			@Override
			public void run() {

				synchronized (this) {
					for (int i = 0; i < ticketNumber; i++) {
						try {
							Thread.sleep(1000);
							System.out.println(Thread.currentThread().getName() + " loop " + i);
						} catch (InterruptedException e) {
							e.getMessage();
						}
					}
				}

			}
		}

		// 启动3个线程t1,t2,t3(它们共用一个Runnable对象)，
		//这3个线程一共卖10张票！这说明它们是共享了MyRunnable接口的。
		Runnable runnable = new MyRunnable();
		Thread t1 = new Thread(runnable, "t1");
		Thread t2 = new Thread(runnable, "t2");
		Thread t3 = new Thread(runnable, "t3");

		t1.start();
		t2.start();
		t3.start();
	}
}
